package IO流;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class 文件复制 {
    static int cachelod = 20000;
    public static void main(String[] args) throws IOException {
        String Of="C:\\Users\\User\\Desktop\\七原罪 第三季(诸神的逆鳞) 第04话 1080P -在线观看&动漫下载 - AGE动漫.mp4";
        String Nf="C:\\Users\\User\\Desktop\\22.mp4";
        File ofile = new File(Of);
        int t1 = 0;
        long a = ofile.length();
        System.out.println(a);
        cachelod = (int)a/128;
        a = a/cachelod;//分缓冲区块的数量
        int b = (int) (a/4);//分线程

        Thread t = new Thread(()->{
            try {
                funciton(Of,
                        Nf,
                        0,b*cachelod//多少个块成块的大小
                );
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        Thread t2 = new Thread(()->{
            try {
                funciton(Of,
                        Nf,
                        b*cachelod,b*cachelod*2
                );
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        Thread t3 = new Thread(()->{
            try {
                funciton(Of,
                        Nf,
                        b*cachelod*2,b*cachelod*3
                );
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        Thread t4 = new Thread(()->{
            try {
                funciton(Of,
                        Nf,
                        b*cachelod*3,b*cachelod*4
                );
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        t.start();
//        t2.start();
//        t3.start();
//        t4.start();
    }
    public static void funciton(String Oname,String Nname,int seek,long size) throws IOException {
        File ofile = new File(Oname);
        File nfile = new File(Nname);
        if(nfile.exists()) nfile.createNewFile();
        size = size>ofile.length()?size:ofile.length();
        RandomAccessFile orandomAccessFile = new RandomAccessFile(ofile,"r");
        RandomAccessFile nrandomAccessFile = new RandomAccessFile(Nname,"rw");
        orandomAccessFile.seek(seek);
        nrandomAccessFile.seek(seek);
        byte b[] = new byte[cachelod];//缓冲区大小
        orandomAccessFile.seek(seek);
        for(;(int) orandomAccessFile.getFilePointer()<size;){
        int read = orandomAccessFile.read(b);
        nrandomAccessFile.write(b);
            if(read<0) return;
        }

    }
}
